package apis

import (
	"github.com/gin-gonic/gin"
	"tylnmall/models"
	"github.com/jinzhu/gorm"
	"common/logger"
	"net/http"
	"tylnmall/constant"
	"errors"
	"tylnmall/service"
	"fmt"
)

//模块列表
func ModuleListApi(c *gin.Context) {
	var module []*models.HomeModule
	var err error
	db := c.MustGet("ty").(*gorm.DB)
	if err = db.Find(&module).Error; err != nil {
		logger.Error(err)
		err = errors.New("story not found")
		logger.Error(err)
		c.JSON(http.StatusOK, gin.H{"err_code": constant.ErrorDB, "err_msg": constant.TranslateErrCode(constant.ErrorDB)})
		return
	}
	c.JSON(http.StatusOK, gin.H{"list": module})
}

func ModuleByIdApi(c *gin.Context) {
	type param struct {
		Id int `json:"id" form:"id"`
	}
	var p param
	var module models.HomeModule
	var err error
	if err = c.Bind(&p); err != nil {
		logger.Error(err)
		c.JSON(http.StatusOK, gin.H{"err_code": constant.ErrorParamWrong, "err_msg": constant.TranslateErrCode(constant.ErrorParamWrong)})
		return
	}
	db := c.MustGet("ty").(*gorm.DB)
	if err = db.Where("id = ?", p.Id).Find(&module).Error; err != nil {
		logger.Error(err)
		err = errors.New("module not found")
		logger.Error(err)
		c.JSON(http.StatusOK, gin.H{"err_code": constant.ErrorDB, "err_msg": constant.TranslateErrCode(constant.ErrorDB)})
		return
	}
	c.JSON(http.StatusOK, gin.H{"list": module})
}

func ModuleComListByHomeApi(c *gin.Context) {
	type param struct {
		Id   int `json:"id" form:"id"`
		Type int `json:"type" form:"type"`
	}
	var p param
	var module []*models.HomeModuleCommodity
	var err error
	if err = c.Bind(&p); err != nil {
		logger.Error(err)
		c.JSON(http.StatusOK, gin.H{"err_code": constant.ErrorParamWrong, "err_msg": constant.TranslateErrCode(constant.ErrorParamWrong)})
		return
	}
	db := c.MustGet("ty").(*gorm.DB)
	if err = db.Where("home_module_id = ?", p.Id).Find(&module).Error; err != nil {
		logger.Error(err)
		err = errors.New("module not found")
		logger.Error(err)
		c.JSON(http.StatusOK, gin.H{"err_code": constant.ErrorDB, "err_msg": constant.TranslateErrCode(constant.ErrorDB)})
		return
	}

	if p.Type == 0 {
		var re []*models.Commodity
		var list = []int{}
		for _, m := range module {
			list = append(list, m.ModuleId)
		}
		if err = db.Where("id in (?)", list).Find(&re).Error; err != nil {
			logger.Error(err)
			err = errors.New("module not found")
			logger.Error(err)
			c.JSON(http.StatusOK, gin.H{"err_code": constant.ErrorDB, "err_msg": constant.TranslateErrCode(constant.ErrorDB)})
			return
		}
		c.JSON(http.StatusOK, gin.H{"list": re})
	} else {
		var re []*models.Story
		var list []int
		for _, m := range module {
			list = append(list, m.ModuleId)
		}
		if err = db.Where("id in (?)", list).Find(&re).Error; err != nil {
			logger.Error(err)
			err = errors.New("module not found")
			logger.Error(err)
			c.JSON(http.StatusOK, gin.H{"err_code": constant.ErrorDB, "err_msg": constant.TranslateErrCode(constant.ErrorDB)})
			return
		}
		c.JSON(http.StatusOK, gin.H{"list": re})
	}
}

func ModuleListAndComApi(c *gin.Context) {
	var module []*models.HomeModule
	var err error
	db := c.MustGet("ty").(*gorm.DB)
	if err = db.Find(&module).Error; err != nil {
		logger.Error(err)
		err = errors.New("story not found")
		logger.Error(err)
		c.JSON(http.StatusOK, gin.H{"err_code": constant.ErrorDB, "err_msg": constant.TranslateErrCode(constant.ErrorDB)})
		return
	}
	var lists []*service.List
	for _, m := range module {
		var l service.List
		l.Id = m.Id
		l.Name = m.Name
		l.Type = m.Type
		l.Img = m.Img
		lists = append(lists, &l)
	}
	Lists, err := service.ModuleAndMoList(lists, db)
	fmt.Println(lists)
	fmt.Println(Lists)
	if err != nil {
		logger.Error(err)
		c.JSON(http.StatusOK, gin.H{"err_code": constant.ErrorDB, "err_msg": constant.TranslateErrCode(constant.ErrorDB)})
		return
	}
	c.JSON(http.StatusOK, gin.H{"list": Lists})
}
